package cn.community.care.module.system.service.article;

import cn.community.care.framework.common.exception.ServiceException;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.datapermission.core.annotation.DataPermission;
import cn.community.care.framework.security.core.util.SecurityFrameworkUtils;
import cn.community.care.module.system.controller.admin.article.vo.ArticlePageReqVO;
import cn.community.care.module.system.controller.admin.article.vo.ArticleRespDetailVO;
import cn.community.care.module.system.controller.admin.article.vo.ArticleSaveReqVO;
import cn.community.care.module.system.dal.dataobject.article.ArticleDO;
import cn.community.care.module.system.dal.dataobject.dict.DictDataDO;
import cn.community.care.module.system.dal.dataobject.msg.MsgDO;
import cn.community.care.module.system.dal.dataobject.topic.TopicDO;
import cn.community.care.module.system.dal.dataobject.user.AdminUserDO;
import cn.community.care.module.system.dal.mysql.article.ArticleMapper;
import cn.community.care.module.system.dal.mysql.msg.MsgMapper;
import cn.community.care.module.system.dal.mysql.topic.TopicMapper;
import cn.community.care.module.system.dal.mysql.user.AdminUserMapper;
import cn.community.care.module.system.service.dict.DictDataService;
import com.xingyuv.captcha.util.StringUtils;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import cn.community.care.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 文章 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private TopicMapper topicMapper;

    @Resource
    private AdminUserMapper adminUserMapper;

    @Resource
    private MsgMapper msgMapper;

    @Resource
    private DictDataService dictDataService;

    @Override
    public Long createArticle(ArticleSaveReqVO createReqVO) {
        ArticleDO article = BeanUtils.toBean(createReqVO, ArticleDO.class);
        // 校验文章话题是否存在
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        article.setAuthorId(userId);
        AdminUserDO adminUserDO = adminUserMapper.selectById(userId);
        article.setAuthorName(adminUserDO.getNickname());
        article.setAuthorAvatar(adminUserDO.getAvatar());
        // 插入
        articleMapper.insert(article);
        // 返回
        return article.getId();
    }

    @Override
    public void updateArticle(ArticleSaveReqVO updateReqVO) {
        // 校验存在
        validateArticleExists(updateReqVO.getId());
        // 更新
        ArticleDO updateObj = BeanUtils.toBean(updateReqVO, ArticleDO.class);
        articleMapper.updateById(updateObj);
    }

    @Override
    public void deleteArticle(Long id) {
        // 校验存在
        validateArticleExists(id);
        // 删除
        articleMapper.deleteById(id);
    }

    private void validateArticleExists(Long id) {
        if (articleMapper.selectById(id) == null) {
            throw new ServiceException(400, "文章不存在");
        }
    }

    @Override
    public ArticleDO getArticle(Long id) {
        return articleMapper.selectById(id);
    }

    @DataPermission(enable = false)
    @Override
    public ArticleRespDetailVO getArticleDetail(Long id) {
        ArticleDO articleDO = articleMapper.selectById(id);
        ArticleRespDetailVO respDetailVO = BeanUtils.toBean(articleDO, ArticleRespDetailVO.class);
        //查找对应的作者以及评论内容
        String creator = articleDO.getCreator();
        AdminUserDO adminUserDO = adminUserMapper.selectById(Long.valueOf(creator));
        respDetailVO.setAuthorAvatar(adminUserDO.getAvatar());
        respDetailVO.setAuthorName(adminUserDO.getNickname());
        if (StringUtils.isNotEmpty(respDetailVO.getTopicCode())){
            DictDataDO articleTopic = dictDataService.getDictData("article_topic", respDetailVO.getTopicCode());
            respDetailVO.setTopicName(articleTopic.getLabel());
        }
        return respDetailVO;
    }

    @Override
    public PageResult<ArticleDO> getArticlePage(ArticlePageReqVO pageReqVO) {
        return articleMapper.selectPage(pageReqVO);
    }

    @Override
    public Long getArticleCount() {
        return articleMapper.selectCount();
    }

    @Override
    public Map<String, Long> getArticlePublishTrend() {
        // 计算7天前的日期
        String startDate = LocalDateTime.now().minusDays(7).format(DateTimeFormatter.ISO_LOCAL_DATE);

        return null;
    }

    @Override
    public List<Map<String, Object>> countTotalPriceByDayInLast7Days() {
        return articleMapper.countTotalPriceByDayInLast7Days();
    }

}